<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/8/22
 * Time: 4:25
 * QQ:1467572213
 */

namespace app\ch\controller;

use think\facade\Config;
use think\facade\Request;
use think\facade\Session;
use \app\ch\model\Recharge as Recharge_m;

class Recharge extends Base
{
    public function index()
    {
//        $redirectUrl  = Request::get();
//        $arr = json_decode($redirectUrl['body'],true);
        $recharge_api = Recharge_m::recharge_api();
        $this->assign('recharge_api',$recharge_api);
        return $this->fetch();
    }
    
    public function trc20(){
        $money = input('money');
        $user_id = Session::get('user_id');
        $user_info = \app\ch\model\Base::get_user_info($user_id);
        $data['id'] = $user_info['id'];
        $data['money'] = $money;
        $this->assign('data',$data);
        return $this->fetch();
    }
    
    public function erc20(){
        $money = input('money');
        $user_id = Session::get('user_id');
        $user_info = \app\ch\model\Base::get_user_info($user_id);
        $data['id'] = $user_info['id'];
        $data['money'] = $money;
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function recharge()
    {
        $user_id = Session::get('user_id');
        $user_info = \app\ch\model\Base::get_user_info($user_id);
        $start_recharge = \app\ch\model\Base::get_config($name = 'start_recharge');
        if (Request::isPost()){
            $money = Request::post('money');
            if ($money < $start_recharge) return ['code'=>0,'msg'=>'Minimum recharge:'.$start_recharge];
            $type = Request::post('type');
            switch ($type)
            {
                case 'shineupay':
                    $res = Recharge_m::shineupay($user_info,$money);
                    if ($res != 'error'){
                        return ['code'=>1,'data'=>$res];
                    }else{
                        return ['code'=>0,'msg'=>'System error, please contact customer service!'];
                    }
                    break;
                case 'Razorpay':
                    return ['code'=>0,'msg'=>'Not opened yet'];
                    break;
                case 'Oceanpay':
                    $res = Recharge_m::oceanpay($user_info,$money);
                    if ($res['code'] == 1){
                        return ['code'=>1,'data'=>$res['data']];
                    }else{
                        return ['code'=>0,'msg'=>'System error'];
                    }
                    break;
                case 'Global_pay':
                    $res = Recharge_m::Global_pay($user_info,$money);
                    return $res;
            }

        }
    }
    
    public function upload(){
        //取文件信息
        $arr = $_FILES["img"];
        //var_dump($arr);
        //加限制条件
        //1.文件类型
        //2.文件大小
        //3.保存的文件名不重复
        if(($arr["type"]=="image/jpeg" || $arr["type"]=="image/png" ) && $arr["size"]<10241000 )
        {
        //临时文件的路径
        $arr["tmp_name"];
        //上传的文件存放的位置
        //避免文件重复: 
        //1.加时间戳.time()加用户名.$uid或者加.date('YmdHis')
        //2.类似网盘，使用文件夹来防止重复
        $name = '/upload/img/'.date('YmdHis').$arr["name"];
        $filename = __DIR__ .'/../../../public'.$name;
        //保存之前判断该文件是否存在
          if(file_exists($filename))
          {
            echo "该文件已存在";
          }
          else
          {
          //中文名的文件出现问题，所以需要转换编码格式
          $filename = iconv("UTF-8","gb2312",$filename);
          //移动临时文件到上传的文件存放的位置（核心代码）
          //括号里：1.临时文件的路径, 2.存放的路径
          $a = move_uploaded_file($arr["tmp_name"],$filename);
          
          return ['img'=>$name];
          }
        }
        else
        {
          echo "上传的文件大小或类型不符";
        }
    }





}